<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/10/9
 * Time: 15:48
 */

namespace app\circle\model;

class ThemeModel extends BaseModel
{
    protected $name = 'circle_theme'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = ['theme_content'=>'json'];


    /*读取器*/
    protected function getIsLikeAttr($value,$data){
        $likeModel=new LikeModel();
        return $likeModel->where(['member_id'=>$this->data['memberId'],'theme_id'=>$data['theme_id']])->count();
    }

    protected function getLikeListAttr($value, $data)
    {
        $likeModel=new LikeModel();
        $likeList=$likeModel->where(['theme_id'=>$data['theme_id']])->limit(15)->select();
        foreach ($likeList as $index => $item) {
            $likeList[$index]['member_name']=$item['member_name'];
        }
        return $likeList;
    }

    protected function getReplyAttr($value, $data)
    {
        $model=new ThreplyModel();
        return $model->where('theme_id',$data['theme_id'])->limit(15)->select();
    }
    protected function getThemeImageAttr($value, $data)
    {
        if(empty($value)){
            return [];
        }else{
            if (is_array($value)){
                return $value;
            }
            return explode(',',$value);
        }
    }
    /*修改器*/
    protected function setThemeNameAttr($value, $data)
    {
        return str_replace("\r\n","<br>",$value);
    }
    /**
     * 发布主题
     * @param $data
     */
    public function publish($data){
        $circleModel = new CircleModel();
        $fields = 'dept_id,dept_name,dept_path,circle_id,circle_name,circle_class_id,circle_class_name';
        $circleInfo = $circleModel->field($fields)->where('circle_id',$data['circle_id'])->find();
        if(empty($circleInfo)){
            $this->error = '未找到该圈子！';
            return false;
        }
        if($data['member_id']>0){
            $memberModel = new MemberModel();
            $memberinfo = $memberModel->where('member_id',$data['member_id'])->find();
        }else{
            $this->error = '请先登录！';
            return false;
        }
        $circleMemberModel=new CircleMemberModel();
        $isTop=$circleMemberModel->where(['member_id'=>$memberinfo['member_id'],'circle_id'=>$circleInfo['circle_id']])->value('is_identity');
        $themeData = $circleInfo->toArray();
        $themeData['theme_name']=$data['theme_name'];
        $themeData['theme_type']=$data['theme_type'];
        $themeData['theme_image']=$data['theme_image'];
        $themeData['member_id']=$memberinfo->getData('member_id');
        $themeData['member_name']=$memberinfo->getData('member_name');
        $themeData['member_truename']=$memberinfo->getData('member_truename');
        $themeData['member_avatar']=$memberinfo->getData('member_avatar');
        $themeData['is_top']=$isTop==1?1:0;
        $this->addData($themeData);
        return true;
    }
}